package com.ly.mp.busicen.common.helper;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Collection;
import java.util.Map;
import java.util.UUID;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class StringHelper {
    private static final Logger logger = LoggerFactory.getLogger(StringHelper.class);
    public static final String EMPTY = "";

    public static Boolean IsEmptyOrNull(Object obj) {
        if (obj == null) {
            return true;
        } else if ("".equals(obj)) {
            return true;
        } else {
            if (obj instanceof String) {
                if (((String) obj).length() == 0) {
                    return true;
                }
            } else if (obj instanceof Collection) {
                if (((Collection) obj).size() == 0) {
                    return true;
                }
            } else if (obj instanceof Map && ((Map) obj).size() == 0) {
                return true;
            }

            return false;
        }
    }

    public static String TranString(Object obj) {
        return obj == null ? "" : obj.toString();
    }

    public static String TranString(Object obj, String defaultval) {
        return obj == null ? defaultval : obj.toString();
    }

    public static String TranSeachString(Object obj) {
        if (obj == null) {
            return "";
        } else {
            String result = obj.toString();
            return result.replace("\\", "\\\\").replace("_", "\\_").replace("%", "\\%");
        }
    }

    public static String TranSeachStringNew(Object obj) {
        if (obj == null) {
            return "";
        } else {
            String result = obj.toString();
            return result.replace("\\", "\\\\").replace("_", "\\_").replace("%", "\\%").replace("[", "[[").replace("]",
                    "]]");
        }
    }

    public static String ByteToString(byte[] srcobj) {
        return ByteToString(srcobj, "UTF-16LE");
    }

    public static String ByteToString(byte[] srcObj, String charEncode) {
        String destObj = null;

        try {
            destObj = new String(srcObj, charEncode);
        } catch (Exception var4) {
            logger.error("转换错误：" + var4.getMessage());
        }

        return destObj.replaceAll(" "," ");
    }
        public static String GetGUID() {
            return UUID.randomUUID().toString().replace("-", "");
        }

        public static String compress(String str) throws Exception {
            if (IsEmptyOrNull(str)) {
                return str;
            } else {
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                GZIPOutputStream gzip = new GZIPOutputStream(out);
                gzip.write(str.getBytes());
                gzip.close();
                return out.toString("ISO-8859-1");
            }
        }

        public static String unCompress(String str) throws Exception {
            if (IsEmptyOrNull(str)) {
                return str;
            } else {
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                ByteArrayInputStream in = new ByteArrayInputStream(str.getBytes("ISO-8859-1"));
                GZIPInputStream gzip = new GZIPInputStream(in);
                byte[] buffer = new byte[256];

                int n;
                while ((n = gzip.read(buffer)) >= 0) {
                    out.write(buffer, 0, n);
                }

                return out.toString("GBK");
            }
        }
    }